//
//  AM_HomeViewController.m
//  AppleMarket
//
//  Created by yangy on 18/10/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "AM_HomeViewController.h"

// view
#import "AM_HomeMainView.h"
// viewModel
#import "HomeMainViewModel.h"
// controller
#import "WYWebController.h"
#import "LoginViewController.h"
#import "CM_NavigationViewController.h"
#import "AnnounceListViewController.h"
#import "SettingViewController.h"
#import "AssetDetailViewController.h"
#import "FBAssetViewController.h"
// model
#import "NewsModel.h"
#import "BannerModel.h"
#import "LoginModel.h"

@interface AM_HomeViewController ()
@property (nonatomic, strong) AM_HomeMainView *mainView;
@property (nonatomic, strong) HomeMainViewModel *mainViewModel;
@end

@implementation AM_HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"";
    [self setNagvationBar];
    [self initView];
    [self bindViewModel];
    
    [[[kNotificationCenter rac_addObserverForName:kUserDidLoginNotification object:nil] throttle:1] subscribeNext:^(NSNotification * _Nullable x) {
        [self setNagvationBar];
    }];
    [[kNotificationCenter rac_addObserverForName:kUserDidLoginOutNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        [self setNagvationBar];
    }];
}

- (void) initView
{
    [self.view addSubview:self.mainView];
}

- (void) setNagvationBar
{
    if([PlistCommon loginModel] && [PlistCommon loginModel].UserID != 0){
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.frame = CGRectMake(0, 0, 92, 44);
        leftButton.titleLabel.font = kFont(15);
        [leftButton setTitleColor:kColorWithHex(0x0E304C) forState:UIControlStateNormal];
        [leftButton setImage:[UIImage imageNamed:@"head_icon"] forState:UIControlStateNormal];
        [leftButton setTitle:[UtilsCommon numberSuitScanf:[PlistCommon loginModel].PhoneNumber] forState:UIControlStateNormal];
        [leftButton setImagePosition:Left spacing:12];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    }else{
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        leftButton.frame = CGRectMake(0, 0, 92, 44);
        [leftButton setImage:[UIImage imageNamed:@"header_logo"] forState:UIControlStateNormal];
        self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    }
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(0, 0, 44, 44);
    rightButton.contentEdgeInsets = UIEdgeInsetsMake(0, 16,0, 0);
    [rightButton setEnlargeEdgeWithTop:0 right:10 bottom:10 left:40];
    [rightButton setImage:[UIImage imageNamed:@"nav_set"] forState:UIControlStateNormal];
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        SettingViewController *settiingVC = [[SettingViewController alloc] init];
        [self bh_PushViewController:settiingVC hiddenTabbar:YES];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void) bindViewModel
{
    @weakify(self);
    [self.mainViewModel.newsClickSubject subscribeNext:^(id  _Nullable x) {
        // 跳转公告详情
        @strongify(self);
        if([x isKindOfClass:[NewsModel class]]){
            NewsModel *n = (NewsModel*) x;
            WYWebController *webVC = [WYWebController webControllerWithTitle:n.DocTitle content:n.DocContent];
            [self bh_PushViewController:webVC hiddenTabbar:YES];
        }
    }];
    
    [self.mainViewModel.bannerClickSubject subscribeNext:^(id  _Nullable x) {
        // 跳转banner链接
        @strongify(self);
        if([x isKindOfClass:[BannerModel class]] ){
            BannerModel *b = (BannerModel*)x;
            if(!kStringIsEmpty(b.link)){
                WYWebController *webVC = [WYWebController webControllerWithTitle:b.Title content:b.link];
                [self bh_PushViewController:webVC hiddenTabbar:YES];
            }
        }
    }];
    
    [self.mainViewModel.assetsVM.loginSubject subscribeNext:^(id  _Nullable x) {
        // 登录
        @strongify(self);
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        CM_NavigationViewController *rootVC = [[CM_NavigationViewController alloc] initWithRootViewController:loginVC];
        [self presentViewController:rootVC animated:YES completion:nil];
    }];
    
    [self.mainViewModel.coinVM.assetClickSubject subscribeNext:^(id  _Nullable x) {
        @strongify(self);
        // 用户资产
        if([x integerValue] == 0){
            // 币币账户                                      
            AssetDetailViewController *detailVC = [[AssetDetailViewController alloc] init];
            detailVC.coinModels = self.mainViewModel.coinVM.bbConinModels;
            [self bh_PushViewController:detailVC hiddenTabbar:YES];
        }else if([x integerValue] == 1){
            // 法币账户
            FBAssetViewController *assetVC = [[FBAssetViewController alloc] init];
            [self bh_PushViewController:assetVC hiddenTabbar:YES];
        }else{
            // 合约账户
        }
    }];
    
    [self.mainViewModel.moreClickSubject subscribeNext:^(id  _Nullable x) {
       // 公告列表
        AnnounceListViewController *listVC = [[AnnounceListViewController alloc] init];
        [self  bh_PushViewController:listVC hiddenTabbar:YES];
    }];
    
}

- (AM_HomeMainView *)mainView
{
    if(!_mainView){
        _mainView = [[AM_HomeMainView alloc] initWithFrame:self.view.bounds viewModel:self.mainViewModel];
    }
    return _mainView;
}

- (HomeMainViewModel *)mainViewModel
{
    if(!_mainViewModel){
        _mainViewModel = [[HomeMainViewModel alloc] init];
    }
    return _mainViewModel;
}


@end
